#region Using Statements
using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Security;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
#endregion

namespace SkyBurner
{
    // Console class
    //
    #region Console class
    public class Console : BaseObject
    {
        private const int SCROLL_INCREMENT = 8;

        protected SpriteFont m_spriteFont;
        protected Texture2D m_textureBackground;
        protected bool m_bVisible;
        protected bool m_bEnabled;
        protected int m_nConsoleTop;
        protected int m_nConsoleHeight;
        protected int m_nMaxLineCount;
        protected Queue<String> m_consoleMessages = new Queue<String>();

        public bool Visible
        {
            get { return m_bVisible; }
            set { m_bVisible = value; }
        }

        public bool Enabled
        {
            get { return m_bEnabled; }
            set { m_bEnabled = value; }
        }

        public Console(BaseGame pGame, String pConsoleBackground, String pSpriteFont)
            : base(pGame)
        {
            m_bVisible = false;
            m_bEnabled = true;
            m_nConsoleHeight = m_Game.GraphicsDevice.Viewport.Height / 2;
            m_nConsoleTop = -m_nConsoleHeight;
            m_textureBackground = m_Game.ContentManager.Load<Texture2D>(pConsoleBackground);
            m_spriteFont = m_Game.ContentManager.Load<SpriteFont>(pSpriteFont);

            m_nMaxLineCount = (m_nConsoleHeight / m_spriteFont.LineSpacing) - 1;
        }

        public void Print(String pMessage)
        {
            if (pMessage.Contains("\n"))
            {
                String[] split = pMessage.Split('\n');

                for (int i = 0; i < split.Length; i++)
                {
                    if (m_consoleMessages.Count >= m_nMaxLineCount)
                        m_consoleMessages.Dequeue();

                    m_consoleMessages.Enqueue(split[i]);
                }
            }
            else
            {
                if (m_consoleMessages.Count >= m_nMaxLineCount)
                    m_consoleMessages.Dequeue();

                m_consoleMessages.Enqueue(pMessage);
            }
        }

        public void Update(GameTime gameTime)
        {
            if (m_bVisible)
            {
                if (m_nConsoleTop < 0)
                    m_nConsoleTop += SCROLL_INCREMENT;
            }
            else
            {
                if (m_nConsoleTop > -m_nConsoleHeight)
                    m_nConsoleTop -= SCROLL_INCREMENT;
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (m_nConsoleTop > -m_nConsoleHeight)
            {
                m_Game.SpriteBatch.Begin();

                m_Game.SpriteBatch.Draw(m_textureBackground, new Vector2(0.0f, m_nConsoleTop), new Rectangle(0, 0, m_Game.GraphicsDevice.Viewport.Width, m_nConsoleHeight), Color.White);

                // draw messages...
                String[] consoleMessages = m_consoleMessages.ToArray();
                Vector2 textPosition = new Vector2(0.5f, m_nConsoleTop);
                for (int i = 0; i < consoleMessages.Length; i++)
                {
                    m_Game.SpriteBatch.DrawString(m_spriteFont, consoleMessages[i], textPosition, Color.White);
                    textPosition.Y += m_spriteFont.LineSpacing;
                }
                m_Game.SpriteBatch.End();
            }
        }
    }
    #endregion
}
